<html>

    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

    </head>

    <body>

        输入您的姓名： <input  id="fname"/>
        <script type="text/javascript">
            var e = document.getElementById("fname");
            //onkeyup事件获得元素的全部value
//            e.onkeyup = function() {
//                alert("onkeyup" + event.which + "-" + e.value);
//            }
//            onkeydown和onkeypress事件获得元素的部分value[不包括触发事件所输入的按键]
//            e.onkeydown = function() {
//                alert("onkeydown:" + event.which + "-" + e.value);
//            }
            e.onkeypress = function() {
                 alert("onkeypress" + event.which + "-" + e.value);
            }
        </script>
    </body>
</html>